<?php
class classOrder extends phpajax{
	function input(){
            aread('order_operation');
            //--------------------------------------
            aread('cbDeliveryType');
            aread('cbPaymentType');
            aread('tao_Additional_Text');
        }
	function loading(){}
	
	function main(){
            $_Window      = new classWindow();  
            $_UserSrv     = new classUsersService();
            $_Order_Forms = new classOrderForms();
            $_OrderData   = new classOrderData();
            
            if($this->order_operation==''){
                if($_UserSrv->bIsLogged()){
                    //////////////////////////////////
                    if(_DBG_MODE_=='YES'){
                        $_basket_sess = new classSessions();
                        $_basket_sess_str = $_basket_sess->open('ShopBasket');
                        file_put_contents('plugins/order/results/5.txt', 'Stav session pred zobrazeni formulara objednavky -> '.$_basket_sess_str);
                    }
                    //////////////////////////////////

                    $OrderCode = $_Order_Forms->getOrder();
                    aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',$OrderCode));

                    if(_DBG_MODE_=='YES') file_put_contents('plugins/order/results/10.txt', 'Vypis do okna -> '.$OrderCode);

                    //////////////////////////////////
                    if(_DBG_MODE_=='YES'){
                        $_basket_sess = new classSessions();
                        $_basket_sess_str = $_basket_sess->open('ShopBasket');
                        file_put_contents('plugins/order/results/6.txt', 'Stav session po zobrazeni formulara objednavky -> '.$_basket_sess_str);
                    }
                    //////////////////////////////////
                }
                else
                    //make order from user entered personal data (without registring) here
                    if($_UserSrv->bIsSetUnloggedUser())
                        aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',$_Order_Forms->getOrder()));
                    else
                        aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',$_Order_Forms->getLogin()));
            }
            
            if($this->order_operation=='oo_logout'){
                aprint('flow_container2','');
            }
            
            //test if cbDeliveryType is selected 
            if($this->order_operation=='oo_make_order'){
                if(!$this->isDeliverySelected($this->cbDeliveryType)){
                  aprint('order_error_container',_i18n_order_YOU_HAVE_TO_SELECT_ONE_DELIVERY_TYPE_FROM_LIST_BEFORE_SENDING_ORDER_);
                }
                if(!$this->isPaymentSelected($this->cbPaymentType)){
                  aprint('order_error_container',_i18n_order_YOU_HAVE_TO_SELECT_ONE_PAYMENT_TYPE_FROM_LIST_BEFORE_SENDING_ORDER_);
                }
                //if all is OK next step will try to save order                
                if(
                    $this->isDeliverySelected($this->cbDeliveryType) and 
                    $this->isPaymentSelected($this->cbPaymentType)                     
                ){
                  
                  $_Input = new classInput();

                    //////////////////////////////////
                    if(_DBG_MODE_=='YES'){
                        $_basket_sess = new classSessions();
                        $_basket_sess_str = $_basket_sess->open('ShopBasket');
                        file_put_contents('plugins/order/results/7.txt', 'Stav session tesne pred odoslanim objednavky -> '.$_basket_sess_str);
                    }
                    //////////////////////////////////
                    
                  $Order_ID = $_OrderData->iInsert(
                      trim($_Input->Sterilize($this->cbDeliveryType)), 
                      trim($_Input->Sterilize($this->cbPaymentType)), 
                      trim($_Input->Sterilize($this->tao_Additional_Text))
                  );
                  if($Order_ID>0){
                    //show dialog about successfull operation
                    aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',$_Order_Forms->getResultAfterOrderSent_Successull($Order_ID)));
                  }
                  else
                    aprint('flow_container2',$_Window->ShowByAJAX('flow_container2',$_Order_Forms->getResultAfterOrderSent_Error()));
                }
            }            
	}
        
    private function isDeliverySelected($cbDeliveryType){
        if(
          $cbDeliveryType!='' and
          is_numeric($cbDeliveryType) and
          $cbDeliveryType>0
        )
          return true;
        else
          return false;
    }

    private function isPaymentSelected($cbPaymentType){
        if(
          $cbPaymentType!='' and
          is_numeric($cbPaymentType) and
          $cbPaymentType>0
        )
          return true;
        else
          return false;
    }
}
?>
